using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class PlayerManager :InteractiveDisplay {
	
	public static PlayerManager Instance {
		get;
		
		private set;
		
	}
	
	void Awake() {
		Instance = this;	
	}
		
		
	// Class starts here
	
	
	public Transform playerprefab;	
	
	public Material[] playerMats;
	public Transform[] spawnedPlayers;
	public ItemManager _itemManager;
	
	//private GameOption _gameOptions;
	
	//public int playerNumber = 1;
	//public HUD _hud;
	private Dictionary<ControlSceme, Player> _activeControlSceme = new Dictionary<ControlSceme, Player>();
	
	
	public override void DoSomeThing(KeyCode k) {
		
		foreach (KeyValuePair<ControlSceme, Player> pair in _activeControlSceme) {
			
			if (pair.Key._up == k) {
				pair.Value.Up();
			} 
			if (pair.Key._down == k ) {
				pair.Value.Down();
			} 
			if (pair.Key._left == k ) {
				pair.Value.Left();
			} 
			if (pair.Key._right == k ) {
				pair.Value.Right();
			}
			if (pair.Key._jump == k ) {
				pair.Value.Jump();
			}
			if (pair.Key._space == k ) {
				pair.Value.Space();
			}
		}
	}
	
		public override void DoSomeThing(KeyCode k, ControlSceme c) {
		
		//foreach (KeyValuePair<Player, ControlSceme> pair in _activeControlSceme) {
			
			print (c.ToString() + " " + k.ToString());
		
			if (c._up == k) {
				_activeControlSceme[c].Up();
			} 
			if (c._down == k ) {
				_activeControlSceme[c].Down();
			} 
			if (c._left == k ) {
				_activeControlSceme[c].Left();
			} 
			if (c._right == k ) {
				_activeControlSceme[c].Right();
			}
			if (c._jump == k ) {
				_activeControlSceme[c].Jump();
			}
			if (c._space == k ) {
				_activeControlSceme[c].Space();
			}
		//}
	}
	
	// Use this for initialization
	void Start () {
		GameOption gameOptions = GameOption.Instance;
		//_gameOptions =  GameObject.Find("GameOptions").GetComponent<GameOption>();
		spawnedPlayers = new Transform[gameOptions._playerControlSceme.Length];
		
		
		for (int count = 0; count < gameOptions._playerControlSceme.Length; count++) {
		//foreach (ControlSceme sceme in _gameOptions._playerControlSceme) {
			
			spawnedPlayers[count] = (Transform)Instantiate (playerprefab);
			//spawnedPlayers[0].position = new Vector3(1f,2f,3f);
			spawnedPlayers[count].FindChild("guy8bones").FindChild("Guybody").renderer.material = playerMats[count];
			//go through scene hierachy to find the renderer of player paint him
		
			Player tempPlayer =  spawnedPlayers[count].transform.GetComponent<Player>();
			tempPlayer.SetItemMan(_itemManager);//hands stuff to the player
			tempPlayer._sceme = gameOptions._playerControlSceme[count];
			//_hud.GivePlayers(thisPlayer);
			
			_activeControlSceme.Add(gameOptions._playerControlSceme[count], tempPlayer);
			GameKeyListener.Instance.ActiveMenus.Add ( gameOptions._playerControlSceme[count], this );
			
		}
		
		
		
		/*
		spawnedPlayers[1] = (Transform)Instantiate (playerprefab);
		spawnedPlayers[1].transform.FindChild("guy8bones").FindChild("Guybody").renderer.material = playerMats[1];
					//go through scene hierachy to find the renderer of player paint him
		thisPlayer = spawnedPlayers[1].transform.GetComponent<Player>();
		*/
		//thisPlayer.SetKeys(KeyCode.W,KeyCode.A,KeyCode.D); //this can't work
		
	}
	
	// Update is called once per frame
	void Update () {
	
	}
}
